package com.pmenergy.util;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.pmenergy.model.Cliente;
import com.pmenergy.view.ClienteServiceBean;
 
@FacesConverter("clienteConverter")



public class ClienteConverter implements Converter {
	
	
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
    	
    	
    	if(value != null && value.trim().length() > 0) {
    		ClienteServiceBean clienteServiceBean = (ClienteServiceBean) fc.getExternalContext().getApplicationMap().get("clienteServiceBean");
            List<Cliente> clienteList = clienteServiceBean.getClienteList(); 
            for (Cliente c : clienteList){
	            if (c.getClienteId().toString().equalsIgnoreCase(value)){
	                return c;
	            }
	        }
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            return String.valueOf(((Cliente) object).getClienteId());
        }
        else {
            return null;
        }
    }   
}